package com.lzy.managersystem.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.managersystem.mapper.MenuMapper;
import com.lzy.managersystem.mapper.UserMapper;
import com.lzy.managersystem.pojo.Menu;
import com.lzy.managersystem.pojo.RoleMenu;
import com.lzy.managersystem.pojo.User;
import com.lzy.managersystem.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author qq619
* @description 针对表【sys_menu】的数据库操作Service实现
 * @createDate 2022-07-11 20:47:50
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
        implements MenuService {


    @Resource
    private MenuMapper menuMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<Menu> getAllMenu(String name) {

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByAsc("order_name");
        // 查询所有数据
        List<Menu> list = list(queryWrapper);
        // 找出pid为null的一级菜单
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        // 找出一级菜单的子菜单
        for (Menu menu : parentNodes) {
            // 筛选所有数据中pid=父级id的数据就是二级菜单
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }


    @Override
    public List<Menu> getUserMenu(String username) {
        List<User> list = userMapper.selectUserMenu(username);
        List<Menu> sortMenu = new ArrayList<>();
        for (User roleMenu : list) {
            List<RoleMenu> roleMenuList = roleMenu.getTrole().getRoleMenuList();
            System.out.println(roleMenuList);
            for (RoleMenu menu : roleMenuList) {
                Menu menus = menu.getMenu();
                sortMenu.add(menus);
                System.out.println(menu.getMenu());
            }
        }

//         找出pid为null的一级菜单
        List<Menu> parentNodes = sortMenu.stream().filter(menu -> {
            return menu.getPid() == null;
        }).collect(Collectors.toList());
        // 找出一级菜单的子菜单
        for (Menu menu : parentNodes) {
            // 筛选所有数据中pid=父级id的数据就是二级菜单
            menu.setChildren(sortMenu.stream().filter(m -> {
                return menu.getId().equals(m.getPid());
            }).collect(Collectors.toList()));
        }
        return sortMenu;
    }

    @Override
    public int insterUser(Menu menu) {
        return menuMapper.insertSelective(menu);
    }

    @Override
    public int deleteUser(Integer id) {
        return menuMapper.deleteById(id);
    }

    @Override
    public int updateUser(Menu menu) {
        return menuMapper.updateSelective(menu);
    }

    @Override
    public int deleteBatchUser(List<Integer> ids) {
        return menuMapper.deleteBatchIds(ids);
    }


}




